package com.ht.modules.component.ai.chat.controller;

import com.ht.entity.Result;
import com.ht.modules.component.ai.chat.entity.ChatRequestParams;
import com.ht.modules.component.ai.chat.service.ChatService;
import com.ht.modules.component.ai.embedding.EmbeddingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

@Slf4j
@RestController
@RequestMapping({"/ai"})
public class ChatController {
    @Autowired
    private EmbeddingService embeddingService;
    @Autowired
    private ChatService chatService;

    /**
     * 流式聊天
     *
     * @param chatRequestParams 参数
     */
    @GetMapping(value = {"/streamingChat"}, produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=UTF-8")
    public Flux<?> streamingChat(ChatRequestParams chatRequestParams) {
        return chatService.chatFlux(chatRequestParams);
    }

    /**
     * 重置会话
     *
     * @param chatRequestParams 参数
     */
    @GetMapping("/resetChat")
    public Result<?> resetChat(ChatRequestParams chatRequestParams) {
        return Result.ok(chatService.resetChat(chatRequestParams));
    }


    /**
     * 向量检索
     *
     * @param message 消息
     */
    @GetMapping({"/vectorstore/query"})
    public Result<?> vectorStoreQuery(String message) {
        return Result.ok(embeddingService.vectorStoreQuery(message, "disease", 0.85, 1));
    }

    /**
     * 文件嵌入
     * TODO 将知识库与会话绑定
     *
     * @param file 文件
     */
    @PostMapping({"/embedding"})
    public Result<?> fileEmbedding(@RequestParam MultipartFile file) {
        this.embeddingService.fileEmbedding(file);
        return Result.ok();
    }

}
